package com.lmpop.trade.ti_member;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;

public class Ti_memberInfo implements TradeInterf
{
  QueryList queryList;

  public Ti_memberInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_member ti_member = new Ti_member();
    ti_member.setCust_id(map.getString("cust_id"));
    if ((map.getString("cust_name") != null) && (!"".equals(map.getString("cust_name"))))
      ti_member.setCust_name(map.getString("cust_name"));
    else {
      ti_member.setCust_name(map.getString("user_name"));
    }
    ti_member.setCust_type(map.getString("cust_type"));
    ti_member.setState_code(map.getString("state_code"));
    ti_member.setUser_class(map.getString("user_class"));

    String result_code = "0";
    try {
      insert(dbmMgr, ti_member);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);

    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_member ti_member)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_member", ti_member);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ti_member where cust_id=#value#
    dbmMgr.update("deleteTi_member", pkid);
  }

  public SysMap updatemember_state(DbmInterf dbmMgr, SysMap map) {
    Ti_member ti_member = new Ti_member();
    ti_member.setCust_id(map.getString("cust_id"));
    String result_code = "0";
    try {
      updatemember_state(dbmMgr, ti_member);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatemember_state(DbmInterf dbmMgr, Ti_member ti_membe) throws ApplicationException
  {
    dbmMgr.update("updatemember_state", ti_membe);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_member ti_member = new Ti_member();
    ti_member.setCust_id(map.getString("cust_id"));
    ti_member.setCust_name(map.getString("cust_name"));
    ti_member.setCust_type(map.getString("cust_type"));

    ti_member.setUser_class(map.getString("user_class"));
    ti_member.setReg_date(map.getString("reg_date"));
    if (map.getString("user_class").equals("9"))
    {
      ti_member.setBy1(map.getString("by1"));
    }

    String result_code = "0";
    try {
      update(dbmMgr, ti_member);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_member ti_member)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_member", ti_member);
  }

  public SysMap updateby1(DbmInterf dbmMgr, SysMap map) {
    Map inmap = new Hashtable();
    inmap.put("cust_id", map.getString("cust_id"));
    inmap.put("by1", map.getString("by1"));
    String result_code = "0";
    try {
      updateby1(dbmMgr, inmap);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateby1(DbmInterf dbmMgr, Map map)
    throws ApplicationException
  {
    dbmMgr.update("upateMemberBy1", map);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Map ti_member = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_member.put("start", Integer.valueOf(start));
    ti_member.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getListByPersonalPage", ti_member);
  }
  public List getListByCompanyPage(Object obj, int start, int limit) {
    Map ti_member = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_member.put("start", Integer.valueOf(start));
    ti_member.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getListByCompanyPage", ti_member);
  }

  public List getLawyerListByPage(Object obj, int start, int limit) {
    Map ti_member = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_member.put("start", Integer.valueOf(start));
    ti_member.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getLawyerListByPage", ti_member);
  }

  public List getListByPageCom(Object obj, int start, int limit) {
    Map ti_member = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_member.put("start", Integer.valueOf(start));
    ti_member.put("limit", Integer.valueOf(limit));
    if ((ti_member.get("user_name") != null) && (!ti_member.get("user_name").toString().equals(""))) return this.queryList.getList("getListByUserNameTi_memberPageCom", ti_member);
    return this.queryList.getList("getListByTi_memberPageCom", ti_member);
  }

  public int getCountByObj(Object obj)
  {
    Map ti_member = (Hashtable)obj;
    List list = this.queryList.getList("getCountByObjTi_member", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public int getCountByObjCom(Object obj) {
    Map ti_member = (Hashtable)obj;
    List list = this.queryList.getList("getCountByObjTi_memberCom", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getList_credit_ByPage(Object obj, int start, int limit)
  {
    Map ti_member = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_member.put("start", Integer.valueOf(start));
    ti_member.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getListByTi_memberPage_credit", ti_member);
  }

  public int getCount_credit_ByObj(Object obj)
  {
    Map ti_member = (Hashtable)obj;
    List list = this.queryList.getList("getCount_credit_ByObj", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_memberByPk", pk);
  }

  public SysMap upateState(DbmInterf dbmMgr, SysMap map)
  {
    Map state_code_map = new Hashtable();
    String pkid_string = map.getString("pkid");
    String by5 = map.getString("by5");
    String state_code = map.getString("up_operating");
    state_code_map.put("state_code", state_code);
    state_code_map.put("by5", by5);
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals("")) {
            state_code_map.put("cust_id", pkStr[i]);
            upateState(dbmMgr, state_code_map);
          }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void upateState(DbmInterf dbmMgr, Map state_code_map)
    throws ApplicationException
  {
    dbmMgr.update("upateMemberState", state_code_map);
  }

  public String getCusttypeByPk(String pk)
  {
    String cust_type = "";
    List list = this.queryList.getList("getCusttypeByPk", pk);
    if ((list != null) && (list.size() > 0)) {
      Map map = (Hashtable)list.get(0);
      if ((map.get("cust_type") != null) && (!map.get("cust_type").equals(""))) {
        cust_type = map.get("cust_type").toString();
      }
    }
    return cust_type;
  }

  public String getCustNameByPk(String pk)
  {
    String cust_name = "";
    List list = this.queryList.getList("getCustNameByPk", pk);
    if ((list != null) && (list.size() > 0)) {
      Map map = (Hashtable)list.get(0);
      if ((map.get("cust_name") != null) && (!map.get("cust_name").equals(""))) {
        cust_name = map.get("cust_name").toString();
      }
    }
    return cust_name;
  }

  public String getCustClassByPk(String pk)
  {
    String user_class = "";
    List list = this.queryList.getList("getTi_memberByPk", pk);
    if ((list != null) && (list.size() > 0)) {
      Map map = (Hashtable)list.get(0);
      if ((map.get("user_class") != null) && (!map.get("user_class").equals(""))) {
        user_class = map.get("user_class").toString();
      }
    }
    return user_class;
  }

  public List getCustNameinfo()
  {
    return this.queryList.getList("getCustNameinfo", "");
  }

  public String getAreaAttrByCustId(String cust_id)
  {
    List list = this.queryList.getList("getAreaAttrByPersonal", cust_id);
    if ((list == null) || (list.size() == 0)) list = this.queryList.getList("getAreaAttrByCompany", cust_id);
    Hashtable map = new Hashtable();
    String area_attr = "";
    if ((list != null) && (list.size() > 0)) map = (Hashtable)list.get(0);
    if (map.get("area_attr") != null) area_attr = map.get("area_attr").toString();
    return area_attr;
  }

  public SysMap alterName(DbmInterf dbmMgr, SysMap map)
  {
    Ti_member member = new Ti_member();
    member.setCust_id(map.getString("cust_id"));
    member.setCust_name(map.getString("user_name"));
    String result_code = "0";
    try {
      dbmMgr.update("updateTi_memberName", member);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public SysMap upateassignsale(DbmInterf dbmMgr, SysMap map)
  {
    Map assign_map = new Hashtable();
    String user_id = "";
    String pkid_string = map.getString("pkid");
    String assign_state = map.getString("assign_state");
    if (assign_state.equals("0")) user_id = map.getString("user_id");

    assign_map.put("user_id", user_id);
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals("")) {
            assign_map.put("cust_id", pkStr[i]);
            upateassignsale(dbmMgr, assign_map);
          }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void upateassignsale(DbmInterf dbmMgr, Map assign_map)
    throws ApplicationException
  {
    dbmMgr.update("upateAssignSale", assign_map);
  }
}